<template>
  <ScUploader
    class="styled-uploader"
    action="http://localhost:3000/api/utils/upload-img"
    :showUploadList="false"
    :beforeUpload="commonUploadCheck"
    drag="true"
    @success="
        (data:SuccessEmit) => {
          handleUploadSuccess(data.resp, data.file.raw)
        }
      "
  >
    <div class="uploader-container">
      <FileImageOutlined />
      <h4>{{ text }}</h4>
    </div>
    <template #loading>
      <div class="uploader-container">
        <LoadingOutlined spin />
        <h4>上传中</h4>
      </div>
    </template>
    <template #uploaded="dataProps">
      <div class="uploader-container">
        <img :src="dataProps.uploadedData.data.urls[0]" v-if="showUploaded" />
        <template v-else>
          <FileImageOutlined />
          <h4>{{ text }}</h4>
        </template>
      </div>
    </template>
  </ScUploader>
</template>
<script setup lang="ts">
import { FileImageOutlined, LoadingOutlined } from '@ant-design/icons-vue'
import { type SuccessEmit, type ErrorEmit } from '@/components/ScUploader/ScUploader.vue'
import { commonUploadCheck } from '@/util/utils'
import { type UploadResp } from '@/types/respTypes'
interface Props {
  text: string
  showUploaded: boolean
}
const props = withDefaults(defineProps<Props>(), {
  text: '上传图片',
  showUploaded: false
})
const emit = defineEmits<{
  (e: 'success', data: any): void
}>()
const handleUploadSuccess = (resp: UploadResp, file: File) => {
  emit('success', { resp, file })
}
</script>

<style lang="scss" scoped>
.styled-uploader {
  .uploader-container {
    width: 100px;
    padding: 10px;
    color: #ffffff;
    background: #1890ff;
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: 5px;
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
  }
  .uploader-container:hover {
    background: #40a9ff;
  }
  .uploader-container h4 {
    color: #ffffff;
    margin-bottom: 0;
    margin-left: 10px;
  }
  .uploader-container img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
}
</style>
